D:\a\cssh-rs\cssh-rs\cssh-rs-protocol\src\v1\keycode.rs
Line | Count | Source |
1 | | //! Key codes and modifier flags for a decoded keypress. |
2 | | |
3 | | use std::ops::BitOr; |
4 | | |
5 | | use serde_derive::{Deserialize, Serialize}; |
6 | | |
7 | | /// Logical key pressed, mirroring the shape of `crossterm::event::KeyCode`. |
8 | | /// |
9 | | /// Adjacently tagged so a key kind added in a later protocol minor version |
10 | | /// decodes to [`KeyCode::Unknown`] instead of failing the whole frame. |
11 | | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
12 | | #[serde(tag = "k", content = "v", rename_all = "snake_case")] |
13 | | pub enum KeyCode { |
14 | | /// The composed character(s) produced by a single keypress. |
15 | | Char(String), |
16 | | /// The Enter / Return key. |
17 | | Enter, |
18 | | /// The Escape key. |
19 | | Escape, |
20 | | /// The Tab key. |
21 | | Tab, |
22 | | /// The Backspace key. |
23 | | Backspace, |
24 | | /// The Delete key. |
25 | | Delete, |
26 | | /// The Insert key. |
27 | | Insert, |
28 | | /// The Up arrow key. |
29 | | Up, |
30 | | /// The Down arrow key. |
31 | | Down, |
32 | | /// The Left arrow key. |
33 | | Left, |
34 | | /// The Right arrow key. |
35 | | Right, |
36 | | /// The Home key. |
37 | | Home, |
38 | | /// The End key. |
39 | | End, |
40 | | /// The Page Up key. |
41 | | PageUp, |
42 | | /// The Page Down key. |
43 | | PageDown, |
44 | | /// A function key; the payload is the `n` in `Fn`. |
45 | | F(u8), |
46 | | /// A modifier key pressed on its own, distinguishing left from right. |
47 | | Modifier(ModifierKeyCode), |
48 | | /// A key not defined in this protocol minor version. |
49 | | #[serde(other)] |
50 | | Unknown, |
51 | | } |
52 | | |
53 | | /// A modifier key pressed on its own, distinguishing the left and right sides. |
54 | | /// |
55 | | /// Mirrors `crossterm::event::ModifierKeyCode`. `Modifiers` reports which |
56 | | /// modifiers were held during another keypress; this reports the modifier key |
57 | | /// itself being pressed or released. |
58 | | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
59 | | #[serde(rename_all = "snake_case")] |
60 | | pub enum ModifierKeyCode { |
61 | | /// Left Shift. |
62 | | LeftShift, |
63 | | /// Right Shift. |
64 | | RightShift, |
65 | | /// Left Control. |
66 | | LeftControl, |
67 | | /// Right Control. |
68 | | RightControl, |
69 | | /// Left Alt. |
70 | | LeftAlt, |
71 | | /// Right Alt (AltGr). |
72 | | RightAlt, |
73 | | /// Left Meta / Super / Windows. |
74 | | LeftMeta, |
75 | | /// Right Meta / Super / Windows. |
76 | | RightMeta, |
77 | | /// A modifier key not defined in this protocol minor version. |
78 | | #[serde(other)] |
79 | | Unknown, |
80 | | } |
81 | | |
82 | | /// Bitflags of the keyboard modifiers held during a keypress. |
83 | | /// |
84 | | /// Encodes on the wire as a single CBOR unsigned integer (the raw bits), so |
85 | | /// modifier bits added in a later minor version round-trip unchanged. |
86 | | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
87 | | #[serde(transparent)] |
88 | | pub struct Modifiers( |
89 | | /// The raw modifier bits; test membership with the [`Modifiers::SHIFT`], |
90 | | /// [`Modifiers::CTRL`], [`Modifiers::ALT`], and [`Modifiers::META`] |
91 | | /// constants. |
92 | | pub u8, |
93 | | ); |
94 | | |
95 | | impl Modifiers { |
96 | | /// No modifiers held. |
97 | | pub const NONE: Modifiers = Modifiers(0); |
98 | | /// The Shift modifier bit. |
99 | | pub const SHIFT: Modifiers = Modifiers(1); |
100 | | /// The Control modifier bit. |
101 | | pub const CTRL: Modifiers = Modifiers(2); |
102 | | /// The Alt modifier bit. |
103 | | pub const ALT: Modifiers = Modifiers(4); |
104 | | /// The Meta / Super / Windows modifier bit. |
105 | | pub const META: Modifiers = Modifiers(8); |
106 | | |
107 | | /// Return true if every bit set in `other` is also set in `self`. |
108 | | /// |
109 | | /// # Arguments |
110 | | /// * `other` - Modifier bits to test for. |
111 | | /// |
112 | | /// # Returns |
113 | | /// `true` when `self` contains all bits of `other`. |
114 | 149 | pub const fn contains(self, other: Modifiers) -> bool { |
115 | 149 | return self.0 & other.0 == other.0; |
116 | 149 | } |
117 | | } |
118 | | |
119 | | impl BitOr for Modifiers { |
120 | | type Output = Modifiers; |
121 | | |
122 | 7 | fn bitor(self, rhs: Modifiers) -> Modifiers { |
123 | 7 | return Modifiers(self.0 | rhs.0); |
124 | 7 | } |
125 | | } |
126 | | |
127 | | #[cfg(test)] |
128 | | #[path = "../tests/v1/test_keycode.rs"] |
129 | | mod tests; |